﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Common.Service;
using SendLeaderMarking.Common.Interface;
using SendLeaderMarking.Common.Entity;

using System.Collections;


namespace SendLeaderMarking
{
    public partial class SendMarking : System.Web.UI.Page
    {
        ISendMarking sendMarking = ServiceContainer.GetService<ISendMarking>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["usercode"] != null)
                {
                    //绑定常用附言
                    IList msgList = sendMarking.GetNormalMsg(Request.QueryString["usercode"].Trim());
                    ddlCommonMsg.DataSource = msgList;
                    ddlCommonMsg.DataTextField = "SETTING_VALUE";
                    ddlCommonMsg.DataTextField = "SETTING_VALUE";
                    ddlCommonMsg.DataBind();
                    ddlCommonMsg.Items.Insert(0, "");
                }
                else
                {
                    Response.Write("<script>alert('缺失usercode参数');</script>");
                }
            }
        }

        //绑定室主管
        protected void rbtnSupervisor_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnSupervisor.Checked)
            {
                if (Request.QueryString["usercode"] != null)
                {
                    IList userList = sendMarking.GetSupervisor(Request.QueryString["usercode"].Trim());
                    lbLeft.Items.Clear();
                    lbLeft.DataSource = userList;
                    lbLeft.DataTextField = "CN_NAME";
                    lbLeft.DataValueField = "USER_CODE";
                    lbLeft.DataBind();
                }
                else
                {
                    Response.Write("<script>alert('缺失usercode参数');</script>");
                }
            }
        }

        //绑定一级部门领导
        protected void rbtnSubDeptManger_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnSubDeptManger.Checked)
            {
                if (Request.QueryString["usercode"] != null)
                {
                    IList userList = sendMarking.GetLeader(Request.QueryString["usercode"].Trim());
                    lbLeft.Items.Clear();
                    lbLeft.DataSource = userList;
                    lbLeft.DataTextField = "CN_NAME";
                    lbLeft.DataValueField = "USER_CODE";
                    lbLeft.DataBind();
                }
                else
                {
                    Response.Write("<script>alert('缺失usercode参数');</script>");
                }
                
            }
        }

        //绑定部门领导
        protected void rbtnDeptManger_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnDeptManger.Checked)
            {
                if (Request.QueryString["usercode"] != null)
                {
                    IList userList = sendMarking.GetTopLeader("Nicky");
                    lbLeft.Items.Clear();
                    lbLeft.DataSource = userList;
                    lbLeft.DataTextField = "CN_NAME";
                    lbLeft.DataValueField = "USER_CODE";
                    lbLeft.DataBind();
                }
                else
                {
                    Response.Write("<script>alert('缺失usercode参数');</script>");
                }
            }
        }

        //绑定公司领导
        protected void rbtnManger_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnManger.Checked)
            {
                if (Request.QueryString["usercode"] != null)
                {
                    IList userList = sendMarking.GetHeadLeader();
                    lbLeft.Items.Clear();
                    lbLeft.DataSource = userList;
                    lbLeft.DataTextField = "CN_NAME";
                    lbLeft.DataValueField = "USER_CODE";
                    lbLeft.DataBind();
                }
                else
                {
                    Response.Write("<script>alert('缺失usercode参数');</script>");
                }
            }
        }

        //添加审批人
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (lbLeft.SelectedItem != null)
            {
                lbRight.Items.Clear();
                lbRight.Items.Add(lbLeft.SelectedItem);
            }
        }

        //移除审批人
        protected void btnDel_Click(object sender, EventArgs e)
        {
            if (lbRight.SelectedItem != null)
            {
                lbRight.Items.Clear();
            }
        }

        //提交审批
        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            if (lbRight.Items.Count > 0)
            {
                string strMsg = "";

                if(ddlCommonMsg.SelectedItem!=null && ddlCommonMsg.SelectedItem.Value!=string.Empty)
                {
                    strMsg = ddlCommonMsg.SelectedItem.Value;
                }
                else
                {
                    if (txtMsg.Text.Length > 0 && txtMsg.Text.Length < 100)
                    {
                        strMsg = txtMsg.Text;
                    }
                    else
                    {
                        Response.Write("<script>alert('附言信息在1-100汉字之间');</script>");
                    }
                }
                ApprovalHistoryEntity histroy = new ApprovalHistoryEntity();
                histroy.HISTORY_ID = Guid.NewGuid().ToString();
                histroy.PROCESS_NAME = "TEST";
                histroy.INCIDENT_NO = 1;
                histroy.TASK_ID = "TEST";//从流程中获取TASKID
                histroy.STEP_NAME = "TEST";//从流程中获取 STEP_NAME
                histroy.ACTION_USER_ID = "TEST";//从流程中获取 USER_ID
                //histroy.ACTION_NAME = "TEST";
                histroy.ACTION_DATE = DateTime.Now;
                histroy.DESTINATION_USER = lbRight.SelectedItem.Value;
                histroy.COPYTO_USER = "TEST";
                histroy.COMMENTS = strMsg;
                histroy.CREATE_DATE = DateTime.Now;
                histroy.CREATE_BY = "TEST";//从流程中获取 USER_ID
                sendMarking.InsertTaskMsg(histroy);
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "openWindow", "openWindow('" + lbRight.SelectedItem.Text + "');",true);
            }
            else
            {
                Response.Write("<script>alert('请选择审批人');</script>");
            }
        }
    }
}